<%@ page language="java" contentType="text/html; charset=UTF-8"
         pageEncoding="UTF-8" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt" %>
<%@ taglib prefix="fn" uri="http://java.sun.com/jsp/jstl/functions" %>
<%
    String path = request.getContextPath();
    String basePath = request.getScheme() + "://"
            + request.getServerName() + ":" + request.getServerPort()
            + path + "/";
%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<base href="<%=basePath%>">
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>公司部门</title>
<link rel="stylesheet" href="static/js/jquery-easyui-1.5.3/themes/default/easyui.css" />
<!-- <link rel="stylesheet" href="static/js/jquery-easyui-1.5.3/themes/bootstrap/easyui.css" /> -->

<link rel="stylesheet" href="static/js/jquery-easyui-1.5.3/themes/icon.css" />
<script type="text/javascript" src="static/js/jquery-easyui-1.5.3/jquery.min.js"></script>
<script type="text/javascript" src="static/js/jquery-easyui-1.5.3/jquery.easyui.min.js"></script>
<script type="text/javascript" src="static/js/jquery-easyui-1.5.3/locale/easyui-lang-zh_CN.js"></script>
</head>
<body>
<div>
<div id="win" class="easyui-panel" title="公司部门" closed="true" style="width:100%;height:650px;">
	<ul id="tt" class="easyui-tree" checkbox="true">
	</ul>
	<a class="easyui-linkbutton" iconCls="icon-add" onclick="save()">保存</a>
<!-- 	<a class="easyui-linkbutton" iconCls="icon-remove" onclick="save()">删除</a> -->
</div>

 <div id="mm" class="easyui-menu" style="width:120px;">
        <div onclick="append()" data-options="iconCls:'icon-add'">新增</div>
        <div data-options="iconCls:'icon-remove'" onclick="deleteInfo()">删除</div>
<!--         <div class="menu-sep"></div> -->
<!--         <div onclick="expand()">Expand</div> -->
<!--         <div onclick="collapse()">Collapse</div> -->
    </div>


</div>
</body>
<script type="text/javascript">
var tt = [];
$(function(){
	
	$('#win').window('open');
	 $("#tt").tree({  
         url:'<%=basePath%>companyAndDepartment/data',//请求路径，id为根节点的id 
         animate: true,
		onClick: function(node){
		  $(this).tree('beginEdit',node.target);
		  
		},
		onAfterEdit:function(node){
			if(node.id==null || node.id==""){
				var nodePar = $("#tt").tree("getParent",node.target);
// 				var level = 
				var obj = {
						  id:node.id,
						  text:node.text,
						  nodeParId:nodePar.id,
						  nodeParText:nodePar.text,
						  state:nodePar.statu
					  }
					  tt.push(obj)
					  
			}
		},
		   onContextMenu: function(e,node){
               e.preventDefault();
               $(this).tree('select',node.target);
               $('#mm').menu('show',{
                   left: e.pageX,
                   top: e.pageY
               });
           },
         onLoadSuccess:function(node,data){  
              var tree = $(this);  
              if(data){  
                  $(data).each(function(index,d) {  
                      if (this.state=='closed') {  
                          tree.tree('expandAll');  
                      }  
                  });  
              }  
         }  
	 });
});
function append(){
	var node = $('#tt').tree('getSelected');
	if (node){
		var nodes = [{
			"id":"",
			"text":""
		}];
		
		$('#tt').tree('append', {
			parent:node.target,
			data:nodes
		});
// 		$('#tt').tree('beginEdit',node.target);
	}
}
function save(){
	var array = JSON.stringify(tt);
	$.ajax({
        type: "POST",
        url: '<%=basePath%>companyAndDepartment/saveCompanyAndDepartmentInfo',
        data: {array:array},
        dataType: 'json',
        //beforeSend: validateData,
        cache: false,
        success: function (data) {
        	$.messager.alert('提示','保存成功');
        	$("#tt").tree('reload');
        },
        error: function(data){
        	$.messager.alert('提示','保存成功');
        	$("#tt").tree('reload');
        }
    });
}
function deleteInfo(){
	var node = $('#tt').tree('getSelected');
	
	$.ajax({
        type: "POST",
        url: '<%=basePath%>companyAndDepartment/deleteInfo',
        data: {id:node.id,state:node.statu},
        dataType: 'json',
        //beforeSend: validateData,
        cache: false,
        success: function (data) {
        	$.messager.alert('提示','保存成功');
        	$("#tt").tree('reload');
        },
        error: function(data){
        	$.messager.alert('提示','保存成功');
        	$("#tt").tree('reload');
        }
    });
}
</script>
</html>